// pages/chinese/pinyin/pinyin.js
// 拼音数据
const pinyinData = {
  "initials": [
    { text: "b", desc: "波", sound: "/sounds/pinyin/b.mp3" },
    { text: "p", desc: "坡", sound: "/sounds/pinyin/p.mp3" },
    { text: "m", desc: "摸", sound: "/sounds/pinyin/m.mp3" },
    { text: "f", desc: "佛", sound: "/sounds/pinyin/f.mp3" },
    { text: "d", desc: "得", sound: "/sounds/pinyin/d.mp3" },
    { text: "t", desc: "特", sound: "/sounds/pinyin/t.mp3" },
    { text: "n", desc: "讷", sound: "/sounds/pinyin/n.mp3" },
    { text: "l", desc: "勒", sound: "/sounds/pinyin/l.mp3" },
    { text: "g", desc: "哥", sound: "/sounds/pinyin/g.mp3" },
    { text: "k", desc: "科", sound: "/sounds/pinyin/k.mp3" },
    { text: "h", desc: "喝", sound: "/sounds/pinyin/h.mp3" },
    { text: "j", desc: "基", sound: "/sounds/pinyin/j.mp3" },
    { text: "q", desc: "欺", sound: "/sounds/pinyin/q.mp3" },
    { text: "x", desc: "希", sound: "/sounds/pinyin/x.mp3" },
    { text: "zh", desc: "知", sound: "/sounds/pinyin/zh.mp3" },
    { text: "ch", desc: "吃", sound: "/sounds/pinyin/ch.mp3" },
    { text: "sh", desc: "诗", sound: "/sounds/pinyin/sh.mp3" },
    { text: "r", desc: "日", sound: "/sounds/pinyin/r.mp3" },
    { text: "z", desc: "资", sound: "/sounds/pinyin/z.mp3" },
    { text: "c", desc: "雌", sound: "/sounds/pinyin/c.mp3" },
    { text: "s", desc: "思", sound: "/sounds/pinyin/s.mp3" },
    { text: "y", desc: "衣", sound: "/sounds/pinyin/y.mp3" },
    { text: "w", desc: "乌", sound: "/sounds/pinyin/w.mp3" }
  ],
  "finals": [
    { text: "a", desc: "啊", sound: "/sounds/pinyin/a.mp3" },
    { text: "o", desc: "哦", sound: "/sounds/pinyin/o.mp3" },
    { text: "e", desc: "鹅", sound: "/sounds/pinyin/e.mp3" },
    { text: "i", desc: "衣", sound: "/sounds/pinyin/i.mp3" },
    { text: "u", desc: "乌", sound: "/sounds/pinyin/u.mp3" },
    { text: "ü", desc: "迂", sound: "/sounds/pinyin/v.mp3" },
    { text: "ai", desc: "哀", sound: "/sounds/pinyin/ai.mp3" },
    { text: "ei", desc: "欸", sound: "/sounds/pinyin/ei.mp3" },
    { text: "ui", desc: "威", sound: "/sounds/pinyin/ui.mp3" },
    { text: "ao", desc: "熬", sound: "/sounds/pinyin/ao.mp3" },
    { text: "ou", desc: "欧", sound: "/sounds/pinyin/ou.mp3" },
    { text: "iu", desc: "优", sound: "/sounds/pinyin/iu.mp3" },
    { text: "ie", desc: "耶", sound: "/sounds/pinyin/ie.mp3" },
    { text: "üe", desc: "约", sound: "/sounds/pinyin/ue.mp3" },
    { text: "er", desc: "儿", sound: "/sounds/pinyin/er.mp3" },
    { text: "an", desc: "安", sound: "/sounds/pinyin/an.mp3" },
    { text: "en", desc: "恩", sound: "/sounds/pinyin/en.mp3" },
    { text: "in", desc: "因", sound: "/sounds/pinyin/in.mp3" },
    { text: "un", desc: "温", sound: "/sounds/pinyin/un.mp3" },
    { text: "ün", desc: "晕", sound: "/sounds/pinyin/vn.mp3" },
    { text: "ang", desc: "昂", sound: "/sounds/pinyin/ang.mp3" },
    { text: "eng", desc: "亨", sound: "/sounds/pinyin/eng.mp3" },
    { text: "ing", desc: "英", sound: "/sounds/pinyin/ing.mp3" },
    { text: "ong", desc: "轰", sound: "/sounds/pinyin/ong.mp3" }
  ],
  "wholes": [
    { text: "zhi", desc: "知", sound: "/sounds/pinyin/zhi.mp3" },
    { text: "chi", desc: "吃", sound: "/sounds/pinyin/chi.mp3" },
    { text: "shi", desc: "诗", sound: "/sounds/pinyin/shi.mp3" },
    { text: "ri", desc: "日", sound: "/sounds/pinyin/ri.mp3" },
    { text: "zi", desc: "资", sound: "/sounds/pinyin/zi.mp3" },
    { text: "ci", desc: "雌", sound: "/sounds/pinyin/ci.mp3" },
    { text: "si", desc: "思", sound: "/sounds/pinyin/si.mp3" },
    { text: "yi", desc: "衣", sound: "/sounds/pinyin/yi.mp3" },
    { text: "wu", desc: "乌", sound: "/sounds/pinyin/wu.mp3" },
    { text: "yu", desc: "迂", sound: "/sounds/pinyin/yu.mp3" },
    { text: "ye", desc: "耶", sound: "/sounds/pinyin/ye.mp3" },
    { text: "yue", desc: "约", sound: "/sounds/pinyin/yue.mp3" },
    { text: "yuan", desc: "渊", sound: "/sounds/pinyin/yuan.mp3" },
    { text: "yin", desc: "因", sound: "/sounds/pinyin/yin.mp3" },
    { text: "yun", desc: "晕", sound: "/sounds/pinyin/yun.mp3" },
    { text: "ying", desc: "英", sound: "/sounds/pinyin/ying.mp3" }
  ]
};

Page({
  data: {
    categoryList: [
      { key: 'initials', name: '声母' },
      { key: 'finals', name: '韵母' },
      { key: 'wholes', name: '整体认读' }
    ],
    currentCategory: '',
    currentPinyins: [],
    audioContext: null
  },

  onLoad() {
    // 创建音频上下文
    this.audioContext = wx.createInnerAudioContext();
    this.audioContext.onEnded(() => {
      // 播放结束
    });
    
    // 默认选择声母
    this.selectCategory({ currentTarget: { dataset: { category: 'initials' } } });
  },

  onUnload() {
    if (this.audioContext) {
      this.audioContext.destroy();
    }
  },

  selectCategory(e) {
    const category = e.currentTarget.dataset.category;
    const pinyins = pinyinData[category] || [];
    
    this.setData({
      currentCategory: category,
      currentPinyins: pinyins
    });
  },

  playPinyin(e) {
    const index = e.currentTarget.dataset.index;
    const pinyin = this.data.currentPinyins[index];
    
    if (pinyin && pinyin.sound && this.audioContext) {
      this.audioContext.src = pinyin.sound;
      this.audioContext.play();
    }
  }
});

